<?php
/**
 * modifier执行器
 *
 * @version $Id: IMintModifierExecutor.php 312 2008-04-23 16:29:38Z iwind $
 * @package util.mint.modifier
 * @since 0.0.2
 */
class IMintModifierExecutor {
	private static $modifiersIncluded = false;
	
	/**
	 * 执行modifier
	 *
	 * @param string $modifierName modifier名称
	 * @param mixed $variable 变量
	 * @param array $arguments 参数集
	 * @return scalar
	 */
	public static function execute($modifierName, $variable, array $arguments) {
		if (!isset(IMintConfig::$modifiers[$modifierName])) {			
			self::load();
			$funcName = "mint_" . str_replace(".", "_", $modifierName) . "_modifier";
			if (function_exists($funcName) && is_callable($funcName)) {// modifier functions
				array_unshift($arguments, $variable);
				return call_user_func_array($funcName, $arguments);
			}
			
			if (function_exists($modifierName) && is_callable($modifierName)) {// system functions
				array_unshift($arguments, $variable);
				return call_user_func_array($modifierName, $arguments);
			}
			
			throw new IMintException("can't find mint modifier by name '{$modifierName}'");
		}
		else { // modifier classes
			$modifierClass = IMintConfig::$modifiers[$modifierName];
			$modifier = new $modifierClass();
			$modifier->setVariable($variable);
			$modifier->setArguments($arguments);
			return $modifier->execute();
		}
	}
	
	/**
	 * Enter description here...
	 *
	 * @since 0.1
	 */
	private static function load() {
		if (self::$modifiersIncluded) {
			return;
		}
		else {
			$file = dirname(__FILE__) . "/IMintCommonModifier.php";
			require_once($file); 
			self::$modifiersIncluded = true;
		}
	}
}

?>